package com.neosavvy.grid.renderer {
    import com.neosavvy.grid.AutoFilteringGrid;

    import com.neosavvy.grid.PickFilterGrid;

    import flash.events.MouseEvent;

    import mx.controls.CheckBox;
    import mx.controls.advancedDataGridClasses.AdvancedDataGridListData;
    import mx.controls.listClasses.BaseListData;
    import mx.controls.listClasses.IDropInListItemRenderer;
    import mx.controls.listClasses.IListItemRenderer;
    import mx.core.IDataRenderer;
    import mx.core.UIComponent;

    public class CenteredCheckBoxHeaderRenderer extends UIComponent implements IDataRenderer, IListItemRenderer, IDropInListItemRenderer {

        public static var CHECKBOX_HEIGHT:int = 16;
        public static var CHECKBOX_WIDTH:int = 16;

        private var selectAllCheckBox:CheckBox;

        private var _listData:BaseListData;

        private var _data:Object;

        private var _adgListData:AdvancedDataGridListData;

        private var _grid:PickFilterGrid;

        protected override function commitProperties():void {
            super.commitProperties();

            if ( _grid && selectAllCheckBox )
            {
                selectAllCheckBox.selected = _grid.selected;                
            }
           
        }

        protected override function createChildren():void {
            super.createChildren();

            if( !selectAllCheckBox )
            {
                selectAllCheckBox = new CheckBox;
                addChild(selectAllCheckBox);
                selectAllCheckBox.addEventListener(MouseEvent.CLICK, handleCheckBoxClickEvent);
            }

        }

        protected override function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void {
            super.updateDisplayList(unscaledWidth, unscaledHeight);

            if ( selectAllCheckBox )
            {
                selectAllCheckBox.setActualSize(CHECKBOX_WIDTH, CHECKBOX_HEIGHT);

                var selectAllCheckBoxX:Number = unscaledWidth / 2 - selectAllCheckBox.getExplicitOrMeasuredWidth() / 2;
                var selectAllCheckBoxY:Number = unscaledHeight / 2 - selectAllCheckBox.getExplicitOrMeasuredHeight() / 2;
                selectAllCheckBox.move( selectAllCheckBoxX, selectAllCheckBoxY);
            }
        }

        private function handleCheckBoxClickEvent(event:MouseEvent):void {

            _grid.toggleSelectDeselectAll();

            event.stopImmediatePropagation();
            event.stopPropagation();

        }


        public function get listData():BaseListData {
            return _listData;
        }

        public function set listData(value:BaseListData):void {
            _listData = value;

            _adgListData = AdvancedDataGridListData(value);
            _grid = PickFilterGrid(_adgListData.owner);
            invalidateProperties();
        }

        public function get data():Object {
            return _data;
        }

        public function set data(value:Object):void {
            _data = value;
        }
    }
}